package com.fast.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fast.admin.service.SysUserService;
import com.fast.admin.utils.SendCodeUtils;
import com.fast.common.db.mapper.SysUserMapper;
import com.fast.common.db.mapper.po.SysUser;
import com.fast.common.pojo.AuthUser;
import com.fast.common.pojo.req.admin.SysUserReq;
import com.fast.common.pojo.resp.admin.SysUserResp;
import com.fast.common.redis.RedisClient;
import com.fast.common.utils.AuthUtils;
import com.fast.common.utils.IdUtil;
import com.fast.common.utils.PropertiesUtil;
import com.fast.common.web.exception.AuthException;
import com.fast.common.web.utils.UserContextHolder;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
* @author TWL
* @description 针对表【sys_user(用户表)】的数据库操作Service实现
* @createDate 2024-10-26 16:43:33
*/
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
    implements SysUserService {

    @Resource
    SendCodeUtils sendCodeUtils;
    @Resource
    SysUserMapper sysUserMapper;
    @Resource
    private AuthUtils authUtils;
    @Resource
    private RedisClient redisClient;

    @Override
    public SysUserResp mailLogin(SysUserReq authUser) {
        return null;
    }

    @Override
    public SysUserResp iphoneLogin(SysUserReq authUser) {
        return null;
    }

    @Override
    public SysUserResp weiChatLogin(SysUserReq authUser) {
        return null;
    }

    @Override
    public SysUserResp passwordLogin(SysUserReq authUser) {

        String code = sendCodeUtils.getCode(authUser.getEmail());
        if (!authUser.getType().equals(code)) {
            Assert.notNull(code, "验证码错误！");
        }

        SysUser user = sysUserMapper.selectOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUsername, authUser.getUsername()).eq(SysUser::getPassword, authUser.getPassword()));
        if (user == null) {
            throw new AuthException("账号密码错误");
        }
        String token = authUtils.build(user.getId());
        AuthUser copy = PropertiesUtil.copy(user, AuthUser.class);
        copy.setToken(token);
        redisClient.set(authUtils.getAccessTokenKey(user.getId()), copy, authUtils.getExpiration(), TimeUnit.SECONDS);

        SysUserResp resp = new SysUserResp();
        resp.setToken(token);
        resp.setUsername(copy.getUsername());
        resp.setEmail(copy.getEmail());
        return resp;
    }

    @Override
    public void register(SysUserReq authUser) {
        SysUser copy = PropertiesUtil.copy(authUser, SysUser.class);
        copy.setId(IdUtil.genSnowId());
        copy.setCreateTime(new Date());
        copy.setModifyId(UserContextHolder.current().getUserId());
        copy.setModifyTime(new Date());
        copy.setCreateId(UserContextHolder.current().getUserId());
        sysUserMapper.insert(copy);

    }
}




